var userCtl = require('../controller/controller.user');
var sellerCtl = require('../controller/controller.seller');

module.exports = {
    //获取用户列表
    getuserList: function (req, res, next) {
        // req.query.teamId = req.session.teamId
        // scheduleSevice.getUserListByTeamId(req, res, function(result){
        //     console.log(result);
        //     next({'list': result.obj});
        // });

        var params = req.query;
        userCtl.getEnableUserList(params, function (err, result) {
            if (err) {

                res.redirect('/error');
                return;
            }
            next({'list': result.obj});
        });

    },
    /**
     * 按照seller查询users
     */
    getUserListBySeller: function (req, res, next) {
        var params = req.query;
        userCtl.getUserListBySeller(params, function (err, result) {
            if (err) {
                res.redirect('/error');
                return;
            }
            next({'list': result.obj});
        });

    },
    //用户详情
    showDetail: function (req, res, next) {
        var param = req.query;

        if (param != null && param.action == 'detail') {
            var id = param.rowId
            userCtl.getInfo({rowId: id}, function (err, result) {
                if (err) {

                    res.redirect('/error');
                    return;
                } else {
                    next({'user': result.obj, 'action': 'detail', 'editable': false});
                }

            });

        } else if (param != null && param.action == 'add') {
            next({'user': null, 'action': 'add', 'editable': true});

        } else {
            next(param);
        }


    },

    //添加用户
    addUser: function (req, res, next) {

        //  var wechat=params.wechat;
        //  var name=params.name;
        //  var phone=params.phone;
        //  var tel=params.tel;
        //  var des=params.des;

        // 'weid',
        //     'name',
        //     'level',
        //     'createUid',
        //     'password',
        //     'enable'
        //
        //
        // scheduleSevice.createUser(req,res,function (result) {
        //
        // });

        var params = req.body;
        if (params == null) {
            res.redirect('/error');
            return;
        }

        var wechat = params.wechat;
        var name = params.name;
        var phone = params.phone;
        var tel = params.tel;
        var des = params.des;


        //var createUid="398daa48-0dc6-4441-bead-3882d507612c";//如何获取创建人的 id
        var createUid = req.session.uid;
        var inputPararm = {
            'name': name,
            'weid': wechat,
            'phone': phone,
            'tel': tel,
            'des': des,
            'level': '2',
            'createUid': createUid,
            'enable': '1',
            'password': wechat
        };

        userCtl.create(inputPararm, function (err, result) {
            if (err) {

                next({'isCreated': 0});
            } else {
                next({'isCreated': 1})
            }

        });
    },
    //删除用户信息
    delUser: function (req, res, next) {

        var params = req.body;
        if (params == null) {
            res.redirect('/error');
            return;
        }

        var rowId = params.rowId;
        userCtl.softDelUer({'rowId': rowId}, function (err, result) {
            if (err) {
                next({'isDel': 0});
            } else {
                next({'isDel': 1});
            }

        })

    },
    //修改用户
    editUser: function (req, res, next) {

        var params = req.body;
        if (params == null) {
            res.redirect('/error');
            return;
        }

        var wechat = params.wechat;
        var name = params.name;
        var phone = params.phone;
        var tel = params.tel;
        var des = params.des;
        var rowId = params.rowId;


        var inputPararm = {'name': name, 'weid': wechat, 'phone': phone, 'tel': tel, 'des': des, 'rowId': rowId};

        userCtl.Update(inputPararm, function (err, result) {
            if (err) {

                next({'isCreated': 0});
            } else {
                next({'isCreated': 1})
            }

        });
    },
    //显示当前用户
    getMyUserInfo: function (req, res, next) {
        userCtl.getInfo({rowId: req.session.uid}, function (err, result) {
            if (err) {
                res.redirect('/error');
                return;
            } else {
                next({'user': result.obj});
            }

        });
    },

    //修改用户密码
    resetPwd: function (req, res, next) {
        userCtl.getInfo({rowId: req.session.uid}, function (err, result) {
            if (err) {
                next({'isUpdate': -1});
                return;
            } else {
                var params = req.body;
                if (params.oldpassword != result.obj.password) {
                    next({'isUpdate': 0});
                    return;
                }

                result.obj.password = params.newpassword

                userCtl.Update(result.obj, function (err, result) {
                    if (err) {
                        next({'isUpdate': -1});
                    } else {
                        req.session.uid = undefined;
                        req.session.userinfo = undefined;
                        next({'isUpdate': 1})
                    }

                });
            }

        });
    }
};

